import React from 'react';
import { history } from 'umi'
import { getWorkOverviewData } from '@/services/organization';

const useOverview = query => {
    const [data, setData] = React.useState([]);
    const [loading, setLoading] = React.useState(true);
    const [mounted, setMounted] = React.useState(false);

    const fetchData = async () => {
        setLoading(true);
        if (mounted) {
            history.push({ pathname: window.location.pathname, query });
        }
        const response = await getWorkOverviewData(query);
        if (response && response.code === 10000) {
            const finalData = { ...response.data };
            Object.keys(finalData).forEach(key => {
                if (finalData[key] == null) {
                    finalData[key] = undefined;
                }
            });
            setData(finalData);
            setMounted(true);
        }
        setLoading(false);
    };

    React.useEffect(() => {
        fetchData();
    }, [query]);

    return [loading, data];
};

export default useOverview;
